﻿<%@ WebHandler Language="C#" Class="SysApplicationGroupsHandler" %>

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;
using System.Web.Script.Serialization;

using log4net;
using log4net.Config;

using Web.Library.DomainObject.SQLServer;
using Web.Library.Facade.SQLServer;
using Web.Library.DataAccess.SQLServer;

using Web.Support;

public class SysApplicationGroupsHandler : BaseHandler {

    private SysApplicationGroupsFacade Facade = new SysApplicationGroupsFacade(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString);


    public override void ProcessRequest(HttpContext context)
    {
        base.ProcessRequest(context);
        context.Response.ContentType = "text/plain";
        string result = Process(context);
        context.Response.Write(result);
    }

    private string Process(HttpContext context)
    {
        string result = string.Empty;
        try
        {
            if (context.Request.QueryString["show"] != null)
            {
                switch (context.Request.QueryString["show"].ToString())
                {

                    case "read": result = this.Read(context); break;
                    case "create": result = this.Create(context); break;
                    case "update": result = this.Update(context); break;
                    case "delete": result = this.Delete(context); break;
                }
            }

            if (context.Request.QueryString["type"] != null)
            {
                switch (context.Request.QueryString["type"].ToString())
                {
                    case "combo": result = this.ComboGroup(context); break;
                    case "list": result = this.GetList(context); break;
                    //case "delete": result = this.DeleteGroups(context); break;
                }
            }


        }
        catch (Exception ex)
        {
            //result = "{success: false, message: 'Error found on page! " + ConfigurationManager.AppSettings["ErrorHelpText"].ToString() + "'}";
            //log.Debug("Process() on GroupHandler.ashx.cs: " + ex.Message.ToString());

            Logger.Debug("Process () SysApplicationGroupsHandler.ashx", ex);
        }
        return result;
    }

    private string GetList(HttpContext context)
    {
        string result = string.Empty;
        List<SysApplicationGroups> list = new List<SysApplicationGroups>();
        try
        {
            list = Facade.GetList();

            JavaScriptSerializer serializer = new JavaScriptSerializer();
            result = serializer.Serialize(list);
        }
        catch (Exception ex)
        {
            throw ex;
        }
        return result;
            
    }
    private string ComboGroup(HttpContext context)
    {
        string result = string.Empty;
        try
        {
            //GroupsFacade groupsFacade = new GroupsFacade();
            //Collection<Groups> allGroups = new Collection<Groups>();
            //allGroups = groupsFacade.RetrieveGroupsList();

            List<SysApplicationGroups> list = Facade.GetList();

            JavaScriptSerializer serializer = new JavaScriptSerializer();
            result = serializer.Serialize(list);
        }
        catch (Exception ex)
        {
            throw ex;
        }

        return result;
    }

    private string Delete(HttpContext context)
    {
        string result = string.Empty;
        try
        {

            Facade.Delete(Convert.ToInt32(context.Request.QueryString["models[0][AppGroupID]"].ToString()));
            result = "{\"success\": \"true\"}";
        }
        catch (Exception ex)
        {
            throw ex;
        }

        return result;
    }

    private string Update(HttpContext context)
    {
        string result = string.Empty;
        try
        {
            SysApplicationGroups obj = new SysApplicationGroups();
            obj.AppGroupID = Convert.ToInt32(context.Request.QueryString["models[0][AppGroupID]"].ToString());
            obj.AppGroupName = context.Request.QueryString["models[0][AppGroupName]"].ToString();
            obj.UpdatedBy = base.UserLoggedIn;
            obj.UpdatedDate = DateTime.Now;
            Facade.Update(obj);
            result = "{\"success\": \"true\"}";
        }
        catch (Exception ex)
        {
            throw ex;
        }

        return result;
    }

    private string Create(HttpContext context)
    {
        string result = string.Empty;
        try
        {

            SysApplicationGroups obj = new SysApplicationGroups();
            obj.AppGroupName = context.Request.QueryString["models[0][AppGroupName]"].ToString();
            obj.CreatedBy = base.UserLoggedIn;
            obj.CreatedDate = DateTime.Now;
            Facade.Save(obj);
            result = "{\"success\": \"true\"}";
        }
        catch (Exception ex)
        {
            throw ex;
        }

        return result;
    }

    private string Read(HttpContext context)
    {
        string result = string.Empty;
        try
        {

            string sort = "asc";
            string dir = "AppGroupID";
            int page = 1;
            int.TryParse(context.Request.QueryString["page"] ?? "1", out page);
            int pageSize = 10;
            int.TryParse(context.Request.QueryString["pageSize"] ?? "10", out pageSize);
            SysApplicationGroups users = new SysApplicationGroups();
            if (context.Request.QueryString["sort[0][dir]"] != null)
                sort = context.Request.QueryString["sort[0][dir]"].ToString();
            if (context.Request.QueryString["sort[0][field"] != null)
                dir = context.Request.QueryString["sort[0][field"].ToString();
            //if (context.Request.QueryString["key"] != null) groups.GroupName = context.Request.QueryString["word"].ToString();


            List<SysApplicationGroups> list = new List<SysApplicationGroups>();
            list = Facade.GetList(pageSize, page, dir, sort);
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            result = "{\"pageSize\": " + list.Count + ", \"data\": " + serializer.Serialize(list) + "}";


        }
        catch (Exception ex)
        {
            throw ex;
        }

        return result;
    }


    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}